import { getSetting, chooseAddress, openSetting, showModal, showToast } from "../../utils/asyncWx.js"
import { request } from "../../request/index.js"
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  data: {
    // 获取url中的get请求参数goods_id
    goods_id: '',
    goodsObj: {
      goods_name: '',
      goods_price: '',
      goods_introduce: '',
      pics: []
    },

    // 轮播图的图片单独存放用于点击预览
    pic: [],
    // 收藏按钮的图标
    icon: "",
    isCollect: ""


  },
  goods_Info: {},
  onLoad: function (options) {
    this.setData({
      goods_id: options.goods_id
    })
    this.getgoodsObj()
    this.setIcon(options.goods_id)

  },
  // 获取商品详情
  async getgoodsObj () {

    // let pic=this.data.pic
    const res = await request({
      url: '/goods/detail?goods_id=' + this.data.goods_id
    })
    // 完整的数据
    this.goods_Info = res
    // 构造要预览的图片数组
    let pic = res.pics.map(item => item.pics_mid)


    this.setData({
      goodsObj: {
        goods_name: res.goods_name,
        goods_price: res.goods_price,
        goods_introduce: res.goods_introduce.replace(/\.webp/g, '.jpg'),
        pics: res.pics
      },
      pic
    })

  },
  // 点击轮播图进行预览
  handlePrivewImage (e) {
    wx.previewImage({
      // 通过事件源获取选中的索引
      current: this.data.pic[e.currentTarget.dataset.pic],
      urls: this.data.pic
    });

  },
  //点击加入购物车
  handleCartAdd (e) {
    // 获取缓存中的购物车数组
    let cart = wx.getStorageSync('cart') || [];
    console.log(this.goods_Info.goods_id);
    //判断商品对象是否存在于购物车数组中
    let index = cart.findIndex(item => item.goods_id == this.goods_Info.goods_id)
    // let index = cart.findIndex(item => item.goods_id === this.goods_Info.goods_id)
    // let index = -1
    let goods_Info = this.goods_Info
    if (index === -1) {
      // 不存在 第一次添加
      goods_Info.num = 1
      //用于控制复选框的选中状态
      goods_Info.checked = true
      // 添加到购物车对象中
      cart.push(this.goods_Info)
    }
    else {
      // 已经存在购物车数据 就执行number++
      cart[index].num++
    }
    this.setData({
      goods_Info
    })
    // 把购物车的数据添加回缓存中
    wx.setStorage({
      key: 'cart',
      data: cart,
    });
    // 弹窗提示
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      // 防止用户手抖疯狂点击按钮
      mask: true,

    });
  },
  // 点击收藏按钮
  handleCollectionTap () {
    let goods_Info = this.goods_Info
    // 从缓存中获取收藏数据
    let collection = wx.getStorageSync('collection') || [];
    // 检查当前的商品是否存在，如果存在就不再添加
    const have = collection.findIndex(item =>
      item.goods_id === goods_Info.goods_id
    );
    // 如果已经存在就取消收藏
    if (have !== -1) {
      collection.splice(have, 1)
      this.setData({
        icon: "icon-shoucang"
      })
      showToast({
        title: "取消收藏成功",
        icon: "success"
      })
    }
    // 不存在就加入收藏
    else {
      collection.push({
        goods_id: goods_Info.goods_id,
        goods_logo: goods_Info.goods_big_logo,
        goods_name: goods_Info.goods_name,
        goods_price: goods_Info.goods_price,
        // goods_icon: "icon-shangcang1"
      })
      let icon = this.data.icon = "icon-shoucang1"
      this.setData({
        icon
      })

      showToast({
        title: "收藏成功",
        icon: "success"
      })
    }
    wx.setStorageSync('collection', collection);
  },
  //设置图标状态
  setIcon (goods_id) {
    let collection = wx.getStorageSync('collection') || [];
    // 不能使用===不然会返回false，因为一个是数字，一个是字符串
    const have = collection.some(item =>
      item.goods_id == goods_id
    );
    console.log(have);
    let icon = ''
    if (have) {
      icon = "icon-shoucang1"
    }
    else {
      icon = "icon-shoucang"
    }
    this.setData({
      icon
    })
  }
  // setState () {
  //   let icon = this.data.icon
  //   if (icon == "shoucang") {
  //     this.setData({
  //       isCollect: "收藏"
  //     })
  //   }
  //   else {
  //     this.setData({
  //       isCollect: "取消收藏"
  //     })
  //   }
  // }

})